//
//  ViewController.m
//  NSOperation
//
//  Created by student on 16/3/24.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "MyOperation.h"

@interface ViewController ()

@property (nonatomic,strong) NSOperationQueue *queue;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 4. 更新界面(向主队列中添加任务)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1.创建一个队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
}

#pragma mark - 3.暂听
//开启一个自定义任务
- (IBAction)btn1Pressed:(id)sender
{
    self.queue = [[NSOperationQueue alloc] init];
    self.queue.maxConcurrentOperationCount = 1;
    [self.queue addOperationWithBlock:^{
        for (int i=0; i<50; i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download1 %@",i,[NSThread currentThread]);
        }
    }];
    [self.queue addOperationWithBlock:^{
        for(int i=0;i<50;i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download2 %@",i,[NSThread currentThread]);
        }
    }];
    [self.queue addOperationWithBlock:^{
        for (int i=0; i<50; i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download3 %@",i,[NSThread currentThread]);
        }
    }];
    
    [self.queue addOperationWithBlock:^{
        for (int i=0; i<50; i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download4 %@",i,[NSThread currentThread]);
        }
    }];
}

//暂停按钮
- (IBAction)btn2Pressed:(UIButton *)sender
{
    //暂停和继续
    self.queue.suspended = !self.queue.isSuspended;
    [sender setTitle:self.queue.suspended?@"继续":@"暂停" forState:UIControlStateNormal];
}


#pragma mark - 2.依赖
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    //1.创建一个队
//    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//
//    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"download 1 %@:%s start!",[NSThread currentThread],__func__);
//    }];
//    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"download 2 %@:%s start!",[NSThread currentThread],__func__);
//    }];
//
//    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"download 3 %@:%s start!",[NSThread currentThread],__func__);
//    }];
//
//    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"download 4 %@:%s start!",[NSThread currentThread],__func__);
//    }];
//
//    NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"download 5 %@:%s start!",[NSThread currentThread],__func__);
//    }];
//    [op4 addDependency:op1];
//    [op4 addDependency:op2];
//    [op4 addDependency:op3];
//    [op4 addDependency:op5];
//    
//    [queue addOperation:op1];
//    [queue addOperation:op2];
//    [queue addOperation:op3];
//    [queue addOperation:op4];
//    [queue addOperation:op5];
//    
//}
#pragma mark - 1. NSOperation 和NSOperationQueue的基本使用
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//    
//    // 2. 创建操作任务 NSOperation的子类
//    
//    // 任务类型1: NSInvocationOperation任务
//    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download1) object:nil];
//    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download2) object:nil];
////    [op1 start];// 操作执行start,不加入队列,则是在当前线程执行
////    [op2 start];
//    
//    // 任务类型2: block任务
//    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"%@:%s start!",[NSThread currentThread],__func__);
//        [NSThread sleepForTimeInterval:0.1];
//        NSLog(@"%@:%s ended!",[NSThread currentThread],__func__);
//    }];
//    
//    [op3 addExecutionBlock:^{
//        NSLog(@"%@:%s start!",[NSThread currentThread],__func__);
//        [NSThread sleepForTimeInterval:0.1];
//        NSLog(@"%@:%s ended!",[NSThread currentThread],__func__);
//    }];
//    
//    
//    // 任务类型3: 自定义NSOperation任务
//    MyOperation *op4 = [[MyOperation alloc] init];
//    
//    [queue addOperation:op1];
//    [queue addOperation:op2];
//    [queue addOperation:op3];
//    [queue addOperation:op4];
//}
//
//- (void)download1
//{
//    NSLog(@"%@:%s start!",[NSThread currentThread],__func__);
//    [NSThread sleepForTimeInterval:0.1];
//    NSLog(@"%@:%s ended!",[NSThread currentThread],__func__);
//}
//
//
//- (void)download2
//{
//    NSLog(@"%@:%s start!",[NSThread currentThread],__func__);
//    [NSThread sleepForTimeInterval:0.1];
//    NSLog(@"%@:%s ended!",[NSThread currentThread],__func__);
//}

@end
